package com.pijunn.report

import java.text.SimpleDateFormat;

import com.pijunn.common.MailingAddress;
import com.pijunn.label.Label;
import com.pijunn.report.Contact;
import com.pijunn.users.AppUser;

import grails.validation.Validateable;


@Validateable
class LostEventCommand {
	def fmt = new SimpleDateFormat('MM/dd/yyyy HH:mm')
	
	def labelService
	
	Integer eventId
	List itemId
	AppUser user
	String wizardStep
	String previous
	
	String description
	String lost
	Date lostDate
	String location
	String venue
	
	Contact contact
	String phone
	Boolean useSms
	MailingAddress mailing = new MailingAddress()
	
	List labeledItemInstanceList
	int labeledItemInstanceTotal
	
	Date foundTime
	
	static constraints = {
		itemId (nullable: true, validator:{val, obj -> 
			if (!val) {
				return 'lost.itemId.empty'
			}
			if (val.size() > 1) {
				return 'lost.itemId.tooMany'
			}
		})
		labeledItemInstanceList (nullable: true, validator:{val, obj -> 
			if (!val && obj.wizardStep == 'select') {
				obj.labeledItemInstanceList = obj.labelService.getMyItems()
				obj.labeledItemInstanceTotal = obj.labeledItemInstanceList.size()
			}
			
			return true
		})
		wizardStep (blank: false)
		lost (nullable: true, validator:{val, obj ->
			if (obj.wizardStep == 'confirm' && !obj.previous) {
				if (!val)
					return 'lost.time.blank.message'
				obj.lostDate = obj.fmt.parse(val)
				if (!obj.lostDate)
					return 'lost.time.format'
			}
		})
		location (nullable: true, validator:{val, obj ->
			if (obj.wizardStep == 'confirm' && !obj.previous && !val)
				return 'lost.location.blank.message'
		})
		venue (nullable: true, validator:{val, obj ->
			if (obj.wizardStep == 'confirm' && !obj.previous && !val)
				return 'lost.venue.blank.message'
		})
		phone (nullable: true, validator:{val, obj ->
			if (obj.wizardStep == 'contact' && !obj.previous && !val)
				return 'lost.contactPhone.blank.message'
			if (obj.wizardStep == 'contact' && !obj.previous && !val.matches(/^[+]?([0-9]*[\.\s\-\(\)]|[0-9]+){3,24}$/))
				return 'lost.contactPhone.matches.invalid'
		})
	}
}
